package com.wnhz.work.yz.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.MyUtils;

/**
 * ---创建密码
 */
public class Register3Activity extends BaseActivity implements View.OnClickListener {
    private TextView tv_right;
    private EditText et_password;

    private String phone;
    private String code;
    private String passWord;

    private ImageView iv_showPwd_new;
    private boolean isPwdShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register3);

        initData();
        initView();
    }

    private void initData() {
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
    }

    private void initView() {
        findViewById(R.id.view_main_middle_title).setVisibility(View.GONE);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.view_main_right_text);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("下一步");
        tv_right.setTextColor(0xff008c99);

        et_password = (EditText) findViewById(R.id.et_password);
        iv_showPwd_new = (ImageView) findViewById(R.id.iv_showPwd_new);
        iv_showPwd_new.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //完成
            case R.id.btn_main_right:
                passWord = et_password.getText().toString();
                if (!"".equals(passWord)) {
                    //^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]
                    if (MyUtils.isPassword(passWord)) {
                    intent = new Intent(this, Register4Activity.class);
                    intent.putExtra("phone", phone);
                    intent.putExtra("code", code);
                    intent.putExtra("passWord", passWord);
                    startActivity(intent);
                    } else {
                        MyToast("密码格式错误，请重新输入!");
                    }
                } else {
                    MyToast("密码不能为空");
                }
                break;

            //显示密码
            case R.id.iv_showPwd_new:
                isPwdShow = !isPwdShow;
                if (isPwdShow) {
                    //设置密码文本为显示的
                    iv_showPwd_new.setImageResource(R.mipmap.ic_xianshimima_li);
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //设置密码文本为隐藏的
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    iv_showPwd_new.setImageResource(R.mipmap.ic_xianshimima_nor);
                }

                et_password.postInvalidate();
//                //切换后将EditText光标置于末尾
//                CharSequence charSequence = login_pwd_new.getText();
//                if (charSequence instanceof Spannable) {
//                    Spannable spanText = (Spannable) charSequence;
//                    Selection.setSelection(spanText, charSequence.length());
//                }
                break;

            default:
                break;
        }
    }
}
